<?php
/**
 * Data Table Column
 * Componente de renderização de colunas do DataGrid
 * @author benrainir
 */
class DataTableColumn
{
    /**
     * Visible
     * @var bool
     */
    private $isVisible = TRUE;
    
    /**
     * Class
     * @var string
     */
    private $class;
    
    /**
     * Name
     * @var string
     */
    private $name;
    
    /**
     * Header
     * @var string
     */
    private $header;
    
    /**
     * isFilter
     * @var bool
     */
    private $isFilter = TRUE;
    
    /**
     * Type
     * sType
     * date, numeric, strings(default), html 
     * @var string
     */
    private $type;
    
    /**
     * Link
     * @var string
     */
    private $link;
    
    /**
     * atributoHtml
     * @var string
     */
    private $atributoHtml;
    
    /**
     * Filter Type
     * @var int
     */
    public $filterType = 0;
    
    /**
     * Target
     * @var string
     */
    public $target = '';
    
    /**
     * Data Type
     * @var string
     */
    public $dataType;
    
    //Filter Types
    const FILTER_TYPE_INPUT = 0;
    const FILTER_TYPE_COMBOBOX = 1;
    
    //Mask Types
    const MASK_CPF = 1;
    const MASK_MONEY = 2;
    
    //Column Types
    const COLUMN_TYPE_DATA = 0;
    const COLUMN_TYPE_ACTION = 1;
    const COLUMN_TYPE_CHECK = 2;
    const COLUMN_TYPE_RADIO = 3;
    const COLUMN_TYPE_TEXT = 4;
    
    public function __construct(
        $name, 
        $header = null, 
        $type = self::COLUMN_TYPE_DATA, 
        $dataType = null, 
        $filterType = self::FILTER_TYPE_INPUT
    )
    {
        $this->name = $name;
        $this->type = $type;
        $this->filterType = $filterType;
        $this->dataType = $dataType;
        $this->setHeader(($header == null) ? $name : $header);
        
        return $this;
    }
    
    public function renderHeader()
    {
    	if (!$this->isVisible) {
    		return null;
    	}
    	 
    	switch($this->getType()){
    		case self::COLUMN_TYPE_CHECK:
    			return '<th class="colunaFixa">
                        <input 
                            type="checkbox" 
                            name="chk" 
                            onclick=\'javascript: checkAllRows("chkRegistro[]", this.checked);\' 
                            />
                        </th>';
    			break;

    		case self::COLUMN_TYPE_RADIO :
    			return '<th></th>';
    			break;

    		default:
    			$dataType = $this->getDataType();
    			$align = (strpos('numeric', $dataType) > -1) ? 'right' : 'left';

    			return "<th align='$align'>{$this->header}</th>";
                break;
            }
    }

    //TODO: Filter utilizar como base a data não filtrada
    public function renderFilter($data)
    {
    	if (!$this->isVisible || !$this->isFilter) {
    		return '';
    	}
    	
        $name = 'fil_'.$this->name;
        $value = (isset($_GET[$name])) ? $_GET[$name] : (isset($_POST[$name])) ? $_POST[$name] : '';
        
        switch ($this->getType()){
        	case self::COLUMN_TYPE_DATA :
                if ($this->filterType == self::FILTER_TYPE_INPUT) {
                    $html = "<th><input type='text' name='$name' id='$name' value='$value' /></th>";
                } else {
                    $arrayColumnData = array();
                    $dataFiltered = array();
                    for ($i = 0; $i < count($data); $i++) {
                        $registro = $data[$i];
                        $arrayColumnData[] = $registro[$this->name]; 
                    }                                           
                        
                    if (count($arrayColumnData) > 0) {
                        $dataFiltered = array_unique($arrayColumnData);
                        sort($dataFiltered);
                    }                        
                       
                    $html  = '<th>';
                    $html .= "<select name='fil_{$this->name}'>";
                    $html .= '<option value=""> - </option>';
                    foreach ($dataFiltered as $d) {
                    	$selected = ($d == $value) ? 'selected="selected"' : '';
                        $html .= "<option value='$d' $selected title='$d'>$d</option>";
                    }
                    $html .= '</select>';
                    $html .= '</th>';
                }
                return $html;          
                break;
        	default:
            	return '<th></th>';
                break;
		}
    }
    
    public function renderBody($data, $key)
    {
    	if (!$this->isVisible) {
    		return '';
    	}
    	
        //Para as colunas Check e Radio calcula-se o value
        $valueExtraColumns = '';           
        $flag = TRUE;
        if ($key) {
			$valueExtraColumns .= ($flag) ? $data[$key] : ';' . $data[$key];
			$flag = FALSE;
        }
         
        switch ($this->getType()) {
        	case self::COLUMN_TYPE_CHECK:
            	return '<td class="colunaFixa">
                            <input 
                                type="checkbox" 
                                name="chkRegistro[]" 
                                value="' . $valueExtraColumns . '" 
                                id="registro' . rand(1, 10000) . '" 
                            ' . $this->atributoHtml . ' />
                        </td>';
                break;
                
            case self::COLUMN_TYPE_RADIO :
                return '<td class="colunaFixa">
                        <input 
                            type="radio" 
                            name="chkRegistro" 
                            value="' . $valueExtraColumns . '" 
                            id="registro'. rand(1, 10000).'" 
                            ' . $this->atributoHtml . ' />
                        </td>';
                break;
                
            case self::COLUMN_TYPE_DATA :
				$dataValue = $data[$this->name];
				$align = (strpos('numeric', $this->getDataType()) > -1) ? 'right' : 'left';
                $class = (strpos('numeric', $this->getDataType()) > -1) ? 'class="colunaNumerica"' : '';
                   
				return "<td align='$align' $class>$dataValue</td>";
                break;
                
            case self::COLUMN_TYPE_ACTION :
               	$html  = '<td align="center" class="colunaFixa">';
                $html .= '<a class="' . $this->class . '"';
                if ($this->target) {
                    $html .= ' target="' . $this->target . '"';
                }
                if ($this->link) {
                 	$html .= " href='{$this->link}$valueExtraColumns'";
                }
                $html .= '>' .  $this->header . '</a></td>';
                return $html;
                break;
                
            case self::COLUMN_TYPE_TEXT:
              	return '<td class="colunaFixa">
                        <input 
                            class="opcional"
                            type="text" 
                            name="' . $data[$this->name] . '" 
                            id="registro'. rand(1, 10000).'" 
                            ' . $this->atributoHtml . ' />
                        </td>';
                break;
                    
            default:
              	return '<td></td>';
                break;
        }
    }

    public function setHeader($value)
    {
        $this->header = strtoupper($value);
    }
    public function getHeader()
    {
        return $this->header;
    }
    
    public function setName($value)
    {
        $this->name = $value;
    }
    
    public function getName()
    {
        return $this->name;
    }
    
    public function setIsVisible($value)
    {
        $this->isVisible = $value;
    }
        
    public function getIsVisible()
    {
        return $this->isVisible;
    }
    
    public function setIsFilter($value)
    {
        $this->isFilter = $value;
    }
        
    public function getIsFilter()
    {
        return $this->isFilter;
    }
    
    public function setClass($value)
    {
        $this->class = $value;
    }
      
    public function getClass()
    {
        return $this->class;
    }
    
    public function setType($value)
    {
        $this->type = $value;
    }
    
    public function getType()
    {
        return $this->type;
    }
    
    public function setLink($value)
    {
        $this->link = $value;
    }
        
    public function getLink()
    {
        return $this->link;
    }
    
    public function setAtributoHtml($value)
    {
        $this->atributoHtml = $value;
    }
    
    public function getAtributoHtml()
    {
        return $this->atributoHtml;
    }   
    
    public function setFilterType($value)
    {
        $this->filterType = $value;
    }
    
    public function getFilterType()
    {
        return $this->filterType;
    }  

    public function setTarget($value)
    {
        $this->target = $value;
    }
    
    public function getTarget()
    {
        return $this->target;
    }  
    
    public function getDataType()
    {
        return $this->dataType;
    }
    
    public function setDataType($value)
    {
        $this->dataType = $value;
    }
}